//
//  UIView+MExtention.swift
//  paperPlane
//
//  Created by 极梵软件 on 2021/1/26.
//

import Foundation
import UIKit

private weak var wty_currentFirstResponder: AnyObject?

extension UIView{
    
    func addShadowToView(_ theView:UIView,_ theColor:UIColor) {
        
        // 阴影颜色
        theView.layer.shadowColor = theColor.cgColor
        
        // 阴影偏移
        theView.layer.shadowOffset = CGSize(width: 0.0, height: 0.0)
        
        // 阴影透明度，默认0
        theView.layer.shadowOpacity = 0.5
        
        // 阴影半径，默认3
        theView.layer.shadowRadius = 5
    }
    
    /// 给View添加圆角
    ///
    /// - Parameters:
    ///   - corners: 哪个角需要添加圆角
    ///   - cornerRadii: 圆角半径
    func addCorners(_ corners:UIRectCorner,cornerRadii:CGSize) {
        
        self.layoutIfNeeded()
        
        let path:UIBezierPath = UIBezierPath.init(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: cornerRadii)
        
        let maskLayer:CAShapeLayer = CAShapeLayer()
        
        maskLayer.frame = self.bounds
        
        maskLayer.path = path.cgPath
        
        self.layer.mask = maskLayer
    }
    
    /// 获取当前View所在的视图控制器
    ///
    /// - Returns: 视图控制器
    func fromViewController()->UIViewController?{
        
        var next:UIResponder? = self.next
        
        var vc:UIViewController?

        repeat {
            
            if ((next as? UIViewController) != nil) {
                
                vc = (next as! UIViewController)
                break
            }else {
                next = next?.next
            }
        } while next != nil
        
        return vc
    }
    
    /// 获取当前View中的第一响应View
    /// - Returns: 可选的第一响应 View
    func findFirstResponder()->UIView?{
        if (self.isFirstResponder) {
              return self
        }
        
        for v in self.subviews {
            if let fistView = v.findFirstResponder(){
                return fistView
            }
        }
        
        return nil
    }
}

extension UIView {
    
    var width : CGFloat{
        
        get{
            return self.frame.width
        }
        
        set{
            var frame = self.frame
            frame.size.width = newValue
            self.frame = frame
        }
    }
    
    var height : CGFloat{
        get{
            return self.frame.height
        }
        
        set{
            var frame = self.frame
            frame.size.height = newValue
            self.frame = frame
        }
    }
    
    var x : CGFloat{
        
        get{
            return self.frame.origin.x
        }
        
        set{
            var frame = self.frame
            frame.origin.x = newValue
            self.frame = frame
        }
    }
    
    var y : CGFloat{
        
        get{
            return self.frame.origin.y
        }
        
        set{
            var frame = self.frame
            frame.origin.y = newValue
            self.frame = frame
        }
    }
    
    var size : CGSize {
        
        get {
            return self.frame.size
        }
        
        set {
            var frame = self.frame
            frame.size = newValue
            self.frame = frame
        }
    }
    
    var centerX : CGFloat{
        
        get {
            return self.center.x
        }
        
        set{
            var center = self.center
            center.x = newValue
            self.center = center
        }
    }
    
    var centerY : CGFloat{
        
        get {
            return self.center.y
        }
        
        set{
            var center = self.center
            center.y = newValue
            self.center = center
        }
    }
}

